package com.winchain.chainbill.bill.biz.send.service.impl;

import com.shell.bee.base.exception.UnCheckMsgException;
import com.shell.bee.base.utils.StringUtil;
import com.winchain.chainbill.ChainBillUtil;
import com.winchain.chainbill.asset.biz.credit.rx.BillAssetTranSubscriber;
import com.winchain.chainbill.basic.context.TimeoutOperation;
import com.winchain.chainbill.bill.base.po.CbOpt;
import com.winchain.chainbill.bill.base.po.CbSnd;
import com.winchain.chainbill.bill.base.service.CbSndService;
import com.winchain.chainbill.bill.biz.send.rx.BatchBillEvent;
import com.winchain.chainbill.bill.biz.send.rx.BillSndRevPassSubscriber;
import com.winchain.chainbill.bill.biz.send.rx.BillSndRevRjctSubscriber;
import com.winchain.chainbill.bill.biz.send.rx.BillSndSignSubscriber;
import com.winchain.chainbill.bill.biz.send.service.BillSndRevService;
import com.winchain.chainbill.bill.biz.send.vo.BatchBillStats;
import com.winchain.chainbill.bill.biz.send.vo.BillSnd;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import rx.Observable;

@Service("billSndRevService")
public class BillSndRevServiceImpl implements BillSndRevService {
	@Autowired
	private CbSndService cbSndService;
	@Autowired
	private BillSndRevPassSubscriber billSndRevPassSubscriber;
	@Autowired
	private BillSndRevRjctSubscriber billSndRevRjctSubscriber;
	@Autowired
	private BillAssetTranSubscriber billAssetTranSubscriber;
	@Autowired
	private BillSndSignSubscriber billSndSignSubscriber;

	public BatchBillStats<CbSnd> pass(String phoneCode, String location, String... billIds) {
		List<BillSnd> records = this.cbSndService.getBillSndByIds(billIds);
		if ((records == null) || (records.size() == 0)) {
			throw new UnCheckMsgException("������������������������");
		}
		BatchBillEvent<BillSnd, CbSnd> event = new BatchBillEvent();
		event.setRecs(records);

		CbOpt opt = new CbOpt();
		opt.setCurState("S001");

		opt.setOptCode("EX02");
		opt.setRemark("������������");

		TimeoutOperation timeoutOpt = new TimeoutOperation();
		timeoutOpt.setOptCode("E004");
		timeoutOpt.setRemark("��������,������������");

		BatchBillStats<CbSnd> stats = new BatchBillStats();

		event.setOperation(opt);
		event.setTimeoutOpt(timeoutOpt);
		event.setStats(stats);
		event.setValue("phoneCode", phoneCode);
		event.setValue("location", location);

		event.setValue("assetEventType", "cb_snd");

		Observable<BatchBillEvent<BillSnd, CbSnd>> observers = Observable.just(event);
		if (ChainBillUtil.signContOnline()) {
			observers.subscribe(this.billSndSignSubscriber);
		}
		observers.subscribe(this.billSndRevPassSubscriber);
		observers.subscribe(this.billAssetTranSubscriber);
		if (ChainBillUtil.onChain()) {
			Object txSnHost = event.getValue("txSnHost");
			if (txSnHost != null) {
				stats.setTxSnHost((String) txSnHost);
			}
		}
		if (billIds.length > records.size()) {
			stats.addAbnormalMsg("��������������������������");
		}
		return stats;
	}

	public BatchBillStats<CbSnd> reject(String remark, String... billIds) {
		List<BillSnd> records = this.cbSndService.getBillSndByIds(billIds);
		if ((records == null) || (records.size() == 0)) {
			throw new UnCheckMsgException("������������������������");
		}
		BatchBillEvent<BillSnd, CbSnd> event = new BatchBillEvent();
		event.setRecs(records);

		CbOpt opt = new CbOpt();
		opt.setCurState("S001");
		opt.setOptCode("E003");
		if (StringUtil.empty(remark)) {
			remark = "������������";
		}
		opt.setRemark(remark);

		BatchBillStats<CbSnd> stats = new BatchBillStats();

		event.setOperation(opt);
		event.setStats(stats);

		Observable<BatchBillEvent<BillSnd, CbSnd>> observers = Observable.just(event);

		observers.subscribe(this.billSndRevRjctSubscriber);
		if (billIds.length > records.size()) {
			stats.addAbnormalMsg("��������������������������");
		}
		return stats;
	}
}
